package com.dayaokeji.rhythmschoolstudent.view;

import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;
import android.text.style.UnderlineSpan;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.dayaokeji.rhythmschoolstudent.R;

/**
 * ********************************************************************
 * *  Created by Fei wo on 2018-12-3
 * ********************************************************************
 * *  version 1.0
 * ********************************************************************
 * *  Copyright (C) dayaokeji.com 2018 -- 2020 All rights reserved.
 * ********************************************************************
 * * 课程评分View
 * ********************************************************************
 */
public class CourseScoringView extends FrameLayout {

    private TextView tvCourseScoring;
    private TextView tvScoringRanking;

    public CourseScoringView(Context context) {
        this(context,null);
    }

    public CourseScoringView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public CourseScoringView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public CourseScoringView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        initView(context);
    }

    private void initView(Context context) {
        View scoringView = LayoutInflater.from(context).inflate(R.layout.view_course_scoring, this, false);
        this.addView(scoringView);
        bindView(scoringView);
    }

    private void bindView(View rootView) {
        tvCourseScoring = rootView.findViewById(R.id.tv_course_scoring);
        tvScoringRanking = rootView.findViewById(R.id.tv_scoring_ranking);
        TextView tvCorseScoringLable = rootView.findViewById(R.id.tv_corse_scroing_label);
        setScoringLabelUnderLine(tvCorseScoringLable);

        setCourseScore(null);
        setScoringRanking(null);
    }

    private void setScoringLabelUnderLine(TextView tvCorseScoringLable) {
        SpannableString scoringLabelSpannable = new SpannableString("课堂表现分");
        UnderlineSpan underlineSpan = new UnderlineSpan();
        scoringLabelSpannable.setSpan(underlineSpan, 0, scoringLabelSpannable.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        tvCorseScoringLable.setText(scoringLabelSpannable);
    }

    public void setCourseScore(Integer score) {
        if (tvCourseScoring != null) {
            String scoreStr;
            if (score == null) {
                scoreStr = "- -";
            }else{
                scoreStr = String.valueOf(score);
            }
            tvCourseScoring.setText(scoreStr);
        }
    }

    public void setScoringRanking(Integer ranking) {
        if (tvScoringRanking != null) {
            String rankingStr;
            if (ranking == null) {
                rankingStr = "- -";
            }else{
                rankingStr = "第"+ranking+"名";
            }
            SpannableString spannableString = new SpannableString(rankingStr);
            ForegroundColorSpan foregroundColorSpan = new ForegroundColorSpan(Color.parseColor("#1C82FE"));
            spannableString.setSpan(foregroundColorSpan, 1, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            RelativeSizeSpan relativeSizeSpan = new RelativeSizeSpan(1.6F);
            spannableString.setSpan(relativeSizeSpan, 1, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            tvScoringRanking.setText(spannableString);
        }
    }
}
